﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmbaocaotinhtranggiaohang : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmbaocaotinhtranggiaohang()
        {
            InitializeComponent();
        }
        #region 1. thoát & nhập lại
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txthanggiao.Text = "";
            txtngaygiao.Text = "";
            txtxe.Text = "";
            cbbthang.ResetText();
            cbbtrangthai.ResetText();
        }
        #endregion
        #region 2. hiển thị báo cáo
        ListViewItem item;
        private void HienThiBCTTGH()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from baocaotinhtranggiaohang,xecongty,trangthai,hanghoa where baocaotinhtranggiaohang.Mxe = xecongty.Mxe and baocaotinhtranggiaohang.Mtrangthai=trangthai.Mtrangthai and baocaotinhtranggiaohang.Mhanghoa=hanghoa.Mhanghoa ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvbctrangthaigiaohang.View = View.Details;
                lvbctrangthaigiaohang.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Mxe"].ToString());
                    item.SubItems.Add(sqlDA["Tenhang"].ToString());
                    item.SubItems.Add(sqlDA["Ngaygiao"].ToString());
                    item.SubItems.Add(sqlDA["Trangthai"].ToString());
                    item.SubItems.Add(sqlDA["Thang"].ToString());
                    lvbctrangthaigiaohang.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }
        private void lvbctrangthaigiaohang_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtxe.Text = item.SubItems[1].Text;
            txthanggiao.Text = item.SubItems[2].Text;
            txtngaygiao.Text = item.SubItems[3].Text;
            cbbtrangthai.Text = item.SubItems[4].Text;
            cbbthang.Text = item.SubItems[5].Text;
        }
        string Mxe, Tenhang, Trangthai;
        int Thang;
        DateTime Ngaygiao;
        private void lvbctrangthaigiaohang_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvbctrangthaigiaohang.Items.Count; i++)
            {
                if (lvbctrangthaigiaohang.Items[i].Selected)
                {
                    Mxe = item.SubItems[1].Text;
                    Tenhang = item.SubItems[2].Text;
                    Ngaygiao =Convert.ToDateTime(item.SubItems[3].Text);
                    Trangthai = item.SubItems[4].Text;
                    Thang = Convert.ToInt32(item.SubItems[5].Text);
                }
            }
        }

        private void frmbaocaotinhtranggiaohang_Load(object sender, EventArgs e)
        {
            HienThiBCTTGH();
        }
        #endregion
        #region 3. cập nhập
        private void btnsua_Click(object sender, EventArgs e)
        {
            string strMxe = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxe.Text + "\'");
            while (dr.Read())
            {
                strMxe = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            string strhanghoa = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select Mhanghoa from hanghoa where Tenhang=N\'" + txthanggiao.Text + "\'");
            while (dr1.Read())
            {
                strhanghoa = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();
            string strtrangthai = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mtrangthai from trangthai where Trangthai=N\'" + cbbtrangthai.Text + "\'");
            while (dr2.Read())
            {
               strtrangthai = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();

            lop_ket_noi.huyketnoi();
            if (txtxe.Text != "" && txthanggiao.Text != "" && txtngaygiao.Text != "" && cbbtrangthai.Text != "" && cbbthang.Text != "")
            {
                lop_bctinhtranggiaohang tinhtrang = new lop_bctinhtranggiaohang(strMxe,strhanghoa,strtrangthai,Convert.ToInt32(cbbthang.Text),Convert.ToDateTime(txtngaygiao.Text));
                if (tinhtrang.capnhat())
                    MessageBox.Show("Quá trình cập nhật báo cáo thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật báo cáo thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin báo cáo! ");
            lvbctrangthaigiaohang.Items.Clear();
            HienThiBCTTGH();
        }
        #endregion
        #region 4. In bao cao
        public static string Xechay = "";
        public static string HG = "";
        public static string NG = "";
        public static string TT = "";
        public static string TBC = "";
        private void btnxuat_Click(object sender, EventArgs e)
        {
            if (txtxe.Text != "" && txthanggiao.Text != "" && txtngaygiao.Text != "" && cbbtrangthai.Text != "" && cbbthang.Text != "")
            {
                Xechay = txtxe.Text;
                HG = txthanggiao.Text;
                NG = Convert.ToString(txtngaygiao.Text);
                TT = cbbtrangthai.Text;
                TBC = Convert.ToString(cbbthang.Text);
                frminbaocaotinhtrang frminbaocao = new frminbaocaotinhtrang();
                frminbaocao.Show();
            }
            else
            { }
        }
        #endregion
    }
}
